﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
public class TeleportObject:ViewableObject
{
    protected new Type thisType;
    protected MissionObject teleporting;
    // The goalTrigger that spawned us
    protected GoalTrigger m_parent;

    public TeleportObject(TeleportObject.Type type, Vector3 placement)
        : base(type, placement)
    {
        thisType = type;
        m_parent = null;
    }

    public void setTeleporting(MissionObject mo)
    {
        if (teleporting == null && mo != null)
        {
            teleporting = mo;
            teleporting.disappear();
            Vector3 newOrigin = (teleporting.collisionMax + teleporting.collisionMin) / 2;
            Vector3 delta = newOrigin-origin;
            max += delta;
            min += delta;
            origin = newOrigin;
        }
    }

    /**
     * Keep a copy of the trigger that spawned us.
     * Use this copy to notify it when we're done teleporting.
     */
    public void setParentTrigger(GoalTrigger gt)
    {
        m_parent = gt;
    }

    public override void update(double elapsed)
    {
        base.update(elapsed);
        if (teleporting != null)
        {
            if (stasis.getFrameNumber() > stasis.getTotalFrames() / 2)
            {
                teleporting = null;
            }
            else
            {
                teleporting.update(elapsed);
            }
        }
        else if(stasis.isFinished)
        {
            cull = true;
            if (m_parent != null)
            {
                m_parent.informTeleportFinished();
            }
        }
    }

    public override void draw()
    {
        base.draw();
        if (teleporting != null)
        {
            teleporting.draw();
        }
    }

    public new class Type:ViewableObject.Type
    {
        public static readonly Type Teleport = new Type(
            ObjectFactory.TypeEnum.Teleport,
            new Vector3(78, 103, 0),
            new Vector3(0, 0, 0),
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(0, 0, 157, 206),
                    0,
                    new Vector2(78, 103)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(157, 0, 157, 206),
                    0,
                    new Vector2(78, 103)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(314, 0, 157, 206),
                    0,
                    new Vector2(78, 103)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(471, 0, 157, 206),
                    0,
                    new Vector2(78, 103)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(628, 0, 157, 206),
                    0,
                    new Vector2(78, 103)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(0, 206, 157, 206),
                    0,
                    new Vector2(78, 85)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(157, 206, 157, 206),
                    0,
                    new Vector2(78, 85)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(314, 206, 157, 206),
                    0,
                    new Vector2(78, 85)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(471, 206, 157, 206),
                    0,
                    new Vector2(78, 85)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.PARTICLES).get("teleportSpriteSheet"),//789x412
                    new Rectangle(628, 206, 157, 206),
                    0,
                    new Vector2(78, 85))},
                    6,//stasis framerate
                    false);//stasis loopable

        protected Type(ObjectFactory.TypeEnum type, Vector3 origin, Vector3 collision,
            ArrayList stasisFrames, int stasisFramerate, Boolean stasisLoopable)
            : base(type, origin, collision, false, stasisFrames, "stasisName", stasisFramerate, stasisLoopable)
        {
        }

        public static new TeleportObject.Type getType(ObjectFactory.TypeEnum te)
        {
            switch (te)
            {
                case ObjectFactory.TypeEnum.Teleport: return Teleport;
            }
            return null;
        }
    }
}
